#include "Window.h"


Window* Window::g_current_win = NULL;

Window::Window():m_window(0,0,1,1)
{
    int col;
    m_window.useColors();
    col = m_window.NumberOfColors();
//    init_pair(1, COLOR_RED, COLOR_BLUE);
//    init_pair(2, COLOR_GREEN, COLOR_RED);
//    init_pair(3, COLOR_BLUE, COLOR_GREEN);
    m_min_h = 2;
    m_min_w = 2;
    SetCurrent();
}

Window::~Window()
{
    if(m_parent){
        m_parent->RemoveWindow(this);
    }
}

//Print actual data of the buffer on the window
int Window::Display(int focus)
{
    m_window.box();
    return 0;
}

//Updates virtual screen buffer
void Window::Update()
{
    m_window.noutrefresh();
}

//Does actual paint on screen
void Window::Paint()
{
    m_window.redraw();
}

void Window::Clean()
{
    m_window.erase();
    m_window.noutrefresh();
}

void Window::AddWindow(IWindow* win)
{
}

void Window::RemoveWindow(IWindow* win)
{
}

void Window::Locate(int y, int x, int h, int w)
{
    IWindow::Locate(y, x, h, w);
    m_window.erase();
    m_window.noutrefresh();
    m_window.wresize(m_h, m_w);
    m_window.mvwin(m_y, m_x);
    Display(true);
    Update();
}

void Window::Resize_LT(int x, int y)
{
    IWindow::Resize_LT(x, y);
    m_window.wresize(m_h, m_w); 
    m_window.mvwin(m_y, m_x);
    m_window.erase();
    Display(true);
    Update();
}

void Window::Resize_RB(int x, int y)
{
    IWindow::Resize_RB(x, y);
    m_window.erase();
    m_window.noutrefresh();
    m_window.wresize(m_h, m_w); 
    m_window.mvwin(m_y, m_x);
    Display(true);
    Update();
}
void Window::Move(int x, int y)
{
    IWindow::Move(x, y);
    m_window.mvwin(m_y, m_x);
}

void Window::Enable()
{
    if(!IsEnabled()) {
        IWindow::Enable();
        m_window.show();
    }
}
void Window::Disable()
{
    if (IsEnabled()) {
        IWindow::Disable();
        m_window.hide();
    }
}

void Window::Top()
{
    if (IsEnabled()) {
        m_window.top();
    }
}

void Window::SetCurrent()
{
    if(g_current_win){
//        g_current_win->m_window.color_set(1);
        g_current_win->Locate(g_current_win->m_y, g_current_win->m_x, g_current_win->m_h, g_current_win->m_w);
    }
    g_current_win = this;
//    m_window.color_set(2);
    if(IsEnabled()){
        Locate(m_y, m_x, m_h, m_w);
        Top();
    }
}
